package com.sinog.biz.schedule;

import com.sinog.biz.mapper.havexml.ReleaseHelpEduMapper;
import com.sinog.biz.util.IdGen;
import com.sinog.core.domain.AbPrisonersJcab;
import com.sinog.core.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *@desc 解除帮教管理-到期自动解除帮教
 *@author jiaobingyang
 *@date 2022/11/28 11:19
 */
@Component
@Slf4j
public class AutoReleaseHelpEduSchedule {

    @Autowired
    ReleaseHelpEduMapper releaseHelpEduMapper;

    /**
     * 解除帮教管理-到期自动解除
     * 解除帮教日期在今天或今天之前的自动解除帮教
     */
    @Scheduled(cron = "${gkzx.azbj.zdjcbj.cron}")
    public void autoRelease(){
        log.info("自动解除帮教开始。。。");
        //1.获取所有解除日期是今天以及今天之前的帮教人员
        List<Map<String,Object>> lists = releaseHelpEduMapper.getAutoReleaseHelpEdu();
        Date date = new Date();
        AbPrisonersJcab abPrisonersJcab;
        //2.状态改为解除帮教
        for (Map<String, Object> map : lists) {
            if (map.get("id") != null){
                //申请中和申请失败的
                abPrisonersJcab = new AbPrisonersJcab();
                abPrisonersJcab.setSpzt("3");
                abPrisonersJcab.setJiechuDate(date);
                abPrisonersJcab.setId((String) map.get("id"));
                abPrisonersJcab.setUpdateUser((String) map.get("account"));
                abPrisonersJcab.setUpdateTime(date);
                abPrisonersJcab.insertOrUpdate();
            } else {
                //未提交申请的
                map.put("jiechu_reason","1");
                map.put("jiechu_remark","");
                map.put("jiechu_date",date);
                map.put("adduserid",map.get("nativevill"));
                map.put("addusername",map.get("sfsname"));
                map.put("superiorid",map.get("pid"));
                map.put("spzt","3");
                String newData = JsonUtil.toText(map);
                //参数实体
                abPrisonersJcab = JsonUtil.toBean(newData,AbPrisonersJcab.class);
                //给新插入的数据一个id
                abPrisonersJcab.setId(IdGen.strid());
                abPrisonersJcab.setCreateUser((String) map.get("account"));
                abPrisonersJcab.setCreateTime(date);
                abPrisonersJcab.setUpdateUser((String) map.get("account"));
                abPrisonersJcab.setUpdateTime(date);
                //入库
                boolean insertOrUpdateResult = abPrisonersJcab.insertOrUpdate();
            }
        }
        log.info("自动解除帮教结束。。。");
    }
}